package cn.itcast.user.web;

import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
//配置文件热部署的开启
@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${pattern.dateformat}")
    private String dateformat;

    /**
     * 读入nacos配置文件中的信息
     *
     * @return
     */
    @GetMapping("now")
    public String now() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
    }

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    /*
     * @HystrixCommand降级：
     *  fallbackMethod属性：指定降级后的方法名称
     * */
    @HystrixCommand(fallbackMethod = "quertById")
    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    public User queryById(@PathVariable("id") Long id) {

        return userService.queryById(id);
    }

    //    定义降级方法
//    1.方法的返回值要和原方法相同
//    2.方法的参数需要和原方法一样
    public User quertById(Long id) {
        User user = new User();
        user.setUsername("这个是服务放的降级");
        return user;
    }
}
